package com.weixiao.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.weixiao.DictFeignClient;
import com.weixiao.enums.DelFlagEnum;
import com.weixiao.enums.DictEnum;
import com.weixiao.enums.YesOrNoEnum;
import com.weixiao.model.hosp.Hospital;
import com.weixiao.repository.HospitalRepository;
import com.weixiao.service.DepartmentService;
import com.weixiao.service.HospitalService;
import com.weixiao.vo.hosp.HospitalQueryVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author 韩诚
 * date 2022/9/20 10:58
 */
@Service
public class HospitalServiceImpl implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;
    @Autowired
    private DictFeignClient dictFeignClient;

    /**
     * 上传医院信息
     * @param paramMap
     */
    @Override
    public void save(Map<String, Object> paramMap) {
        Hospital hospital = JSONObject.parseObject(JSONObject.toJSONString(paramMap), Hospital.class);
        //判断是否存在
        Hospital targetHospital = hospitalRepository.getHospitalByHoscode(hospital.getHoscode());
        if (ObjectUtil.isNotNull(targetHospital)) {
            hospital.setStatus(targetHospital.getStatus());
            hospital.setCreateTime(targetHospital.getCreateTime());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(DelFlagEnum.NO.getCode());
            hospitalRepository.save(hospital);
        } else {
            hospital.setStatus(YesOrNoEnum.YES.getCode());
            hospital.setCreateTime(new Date());
            hospital.setUpdateTime(new Date());
            hospital.setIsDeleted(DelFlagEnum.NO.getCode());
            hospitalRepository.save(hospital);
        }
    }

    /**
     * 根据医院编号查询
     *
     * @param hoscode
     * @return
     */
    @Override
    public Hospital getHospitalByHoscode(String hoscode) {
        return hospitalRepository.getHospitalByHoscode(hoscode);
    }

    /**
     * 查询医院分页列表
     *
     * @param page
     * @param limit
     * @param hospitalQueryVo
     * @return
     */
    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        //0为第一页
        Pageable pageable = PageRequest.of(page - 1, limit);

        //将hospitalQueryVo转为hospital对象
        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);

        //获取查询list集合，遍历进行医院等级封装
        pages.getContent().stream().forEach(item -> {
            this.packHospital(item);
        });
        return pages;
    }

    //更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        Hospital hospital = hospitalRepository.findById(id).get();
        hospital.setStatus(status);
        hospital.setUpdateTime(new Date());
        hospitalRepository.save(hospital);
    }

    //获取医院详情
    @Override
    public Map<String, Object> show(String id) {
        Map<String, Object> map = new HashMap<>();

        //获取医院并调用方法加入医院等级
        Hospital hospital = this.packHospital(hospitalRepository.findById(id).get());
        map.put("hospital", hospital);

        //单独处理更直观
        map.put("bookingRule", hospital.getBookingRule());

        return map;
    }

    /**
     * 封装数据
     *
     * @param hospital
     * @return
     */
    private Hospital packHospital(Hospital hospital) {
        //根据dictCode和value获取医院等级名称
        String hostypeString = dictFeignClient.getName(DictEnum.HOSTYPE.getDictCode(), hospital.getHostype());

        hospital.getParam().put("hostypeString", hostypeString);
        return hospital;
    }

    /**
     * 根据医院编号获取医院名称
     *
     * @param hoscode
     * @return
     */
    @Override
    public String getHosName(String hoscode) {
        Hospital hospital = hospitalRepository.getHospitalByHoscode(hoscode);
        return ObjectUtil.isNotNull(hospital) ? hospital.getHosname() : "";
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        return hospitalRepository.findHospitalByHosnameLike(hosname);
    }

    /**
     * 根据医院编号获取预约挂号详情
     *
     * @param hoscode
     * @return
     */
    @Override
    public Map<String, Object> item(String hoscode) {
        Map<String, Object> result = new HashMap<>();
        //医院详情
        Hospital hospital = this.packHospital(this.getHospitalByHoscode(hoscode));
        result.put("hospital", hospital);
        //预约规则
        result.put("bookingRule", hospital.getBookingRule());
        //不需要重复返回
        hospital.setBookingRule(null);
        return result;
    }
}